package com.situ.blog.interceptor;

import com.situ.blog.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    // pre:previous
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户有没有登录
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //如果没有登录，return false，跳转到登录界面访问
            response.sendRedirect(request.getContextPath() + "/login/getLoginPage");
            return false;
        } else {
            //如果已经登录，return true，访问后面的资源
            return true;
        }
    }
}
